package com.yequan.leetcode.array.reverseString;

/**
 * @author yequan
 * @date 2021/10/31 17:09
 **/
public class ReverseString {

    /**
     * 双指针法
     *
     * @param s
     */
    public void reverseString1(char[] s) {
        if (s == null || s.length == 0) {
            return;
        }
        int left = 0, right = s.length - 1;
        while (left < right) {
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left++;
            right--;
        }
    }

    /**
     * 递归法
     *
     * @param s
     */
    public void reverseString2(char[] s) {
        reverse(s, 0, s.length - 1);
    }

    public void reverse(char[] s, int left, int right) {
        if (left >= right) {
            return;
        }
        char temp = s[left];
        s[left] = s[right];
        s[right] = temp;
        reverse(s, left + 1, right - 1);
    }

}
